package com.mobile.check.activitys;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;

import com.yanzhenjie.permission.Action;
import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.Permission;

import java.util.List;
import java.util.concurrent.TimeUnit;

import cn.droidlover.xdroidmvp.kit.NLog;
import cn.droidlover.xdroidmvp.mvp.XActivity;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import com.mobile.check.app.R;
import com.mobile.check.present.BasePresent;

import com.mobile.check.dialogs.SubmitTipDialog;


public class SplashActivity extends XActivity<BasePresent> {
    @Override
    public BasePresent newP() {
        return new BasePresent();
    }

    @Override
    public void getDataFromServer() {


    }

    @Override
    public int getContentViewId() {
        return R.layout.activity_splash;
    }

    @Override
    public void getIntentData() {
    }

    @Override
    public void initView() {
        super.initView();
    }


    @Override
    public void initData(Bundle savedInstanceState) {
        checkPermission();
    }

    private void startTimer() {
        Observable observable = null;
        observable = Observable.timer(2000, TimeUnit.MILLISECONDS);
        observable.observeOn(AndroidSchedulers.mainThread()).subscribe(new Consumer() {
            @Override
            public void accept(Object o) throws Exception {
                goToMainActivity();
            }
        });
    }


    private void checkPermission() {
        final String[] PHONE = new String[]{Permission.CAMERA,Permission.READ_EXTERNAL_STORAGE, Permission.ACCESS_COARSE_LOCATION, Permission.ACCESS_FINE_LOCATION,
                Permission.WRITE_EXTERNAL_STORAGE, Permission.READ_PHONE_STATE};
        boolean hasPermission = AndPermission.hasPermissions(this, PHONE);
        NLog.e("SplashActivity", "checkPermission hasPermission = " + hasPermission);
        if (hasPermission) {
            startTimer();
        } else {
            // 申请权限。
            AndPermission.with(this)
                    .runtime()
                    .permission(PHONE)
                    .onGranted(new Action<List<String>>() {
                        @Override
                        public void onAction(List<String> data) {
                            for (String Permission : data) {
                                NLog.e("SplashActivity", "onDenied Permission = " + Permission);
                            }
                            startTimer();
                        }
                    }).onDenied(new Action<List<String>>() {
                @Override
                public void onAction(List<String> data) {
                    for (String Permission : data) {
                        NLog.e("SplashActivity", "onGranted Permission = " + Permission);
                    }
                    if (AndPermission.hasAlwaysDeniedPermission(SplashActivity.this, data)) {
                    }
                    startTimer();
                }
            }).start();
        }
    }


    @Override
    protected void onResume() {
        super.onResume();
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public int getTitleLayoutResId() {
        return 0;
    }


    private void goToMainActivity() {
        if (isOutTime()) {
            showUpdateVerson();
        } else {
            startActivity(new Intent(this, Home2Activity.class));
            finish();
        }
    }

    private void showUpdateVerson() {
        SubmitTipDialog tipDialog = new SubmitTipDialog(this, new SubmitTipDialog.SlectClickListener() {
            @Override
            public void submitClick() {
                Intent intent = new Intent();
                intent.setAction("android.intent.action.VIEW");
                Uri content_url = Uri.parse("http://d.firim.pro/shihuaJC");
                intent.setData(content_url);
                startActivity(intent);
            }

            @Override
            public void cancelClick() {

            }
        });
        tipDialog.display("提示", "版本已过期，请下载新版本！", "取消", "下载");
    }


    @Override
    public boolean isNotWorkConnect() {
        return false;
    }
}
